/**
 *  编辑页
 *
 * 作者 admin
 * 日期 2024-08-20 15:49:12
 */
layui.use(['jquery','febs', 'form', 'validate','fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        commonJS = layui.commonJS,
        fileUpload = layui.fileUpload,
        $view = $('#hazardExamineAudit-edit'),
        $hazardOperation = $view.find('#hazardOperation'),
        validate = layui.validate;

    let tenantId = POPUP_DATA.tenantId;
    let examineId = POPUP_DATA.examineId;
    let examineRecordId = POPUP_DATA.examineRecordId;
    let auditRecordList=[];
    initDictItemLabel_rectify();
    initFormValue();
    initRecordAuditTable();
    form.verify(validate);
    form.render();

    if(hazardExamineAuditData){
        if (hazardExamineAuditData.examineFileList && hazardExamineAuditData.examineFileList.length>0) {
            var fileUploadProblem = fileUpload.render({
                elem: '#fileUploadProblem',
                url: '',
                type: 0,//0-图片，1-视频，2-其他
                maxNum:5,
                defaultData: hazardExamineAuditData.examineFileList,
                isDetail: true,
                done: function (res) {
                },
            });
        }

        if (hazardExamineAuditData.rectifyFileList && hazardExamineAuditData.rectifyFileList.length>0) {
            var imgViewBoxRectify = fileUpload.render({
                elem: '#imgViewBoxRectify',
                url: '',
                type: 0,//0-图片，1-视频，2-其他
                maxNum:5,
                defaultData: hazardExamineAuditData.rectifyFileList,
                isDetail: true,
                done: function (res) {
                },
            });
        }
    }
    function initRecordAuditTable() {
        tableIns = febs.table.init({
            elem: $view.find("#recordAuditTable"),
            id: 'recordAuditTable',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'auditRealName', title: '审核人姓名', width: 150},
                {field: 'auditResult',align:'center', title: '审核结果', width: 100, templet: function (d) {
                        return d.auditResult =='1'?'审核通过':'审核退回';
                    }
                },
                {field: 'auditReason', title: '审核理由', minWidth: 260},
                {field: 'createdTime',align:'center', title: '审核时间', width: 180},
            ]],
            done: function(res, curr, count){
            },
            data:auditRecordList
        });
    }
    function initDictItemLabel_rectify(){
        febs.initDictItemLabel($hazardOperation,'SAFETY_ACCIDENT_TYPE',hazardExamineAuditData.hazardOperation);
    }

    function initFormValue() {
        if (hazardExamineAuditData) {
            if(hazardExamineAuditData.auditRecordList && hazardExamineAuditData.auditRecordList.length>0){
                auditRecordList = hazardExamineAuditData.auditRecordList;
            }
            form.val("hazardExamineAudit-edit-form", {
                "examineRecordId": hazardExamineAuditData.examineRecordId,
                "tenantId": hazardExamineAuditData.tenantId,
                "examineId": hazardExamineAuditData.examineId,
                "hazardExamineTitle": hazardExamineAuditData.hazardExamineTitle,
                "hazardOperation": hazardExamineAuditData.hazardOperation,
                "hazardLevel": hazardExamineAuditData.hazardLevel,
                "areaId": hazardExamineAuditData.areaId,
                "areaName": hazardExamineAuditData.areaName,
                "examineEvidence": hazardExamineAuditData.examineEvidence,
                "projectId": hazardExamineAuditData.projectId,
                "projectName": hazardExamineAuditData.projectName,
                "leaderDeptId": hazardExamineAuditData.leaderDeptId,
                "leaderDeptName": hazardExamineAuditData.leaderDeptName,
                "leaderId": hazardExamineAuditData.leaderId,
                "leaderName": hazardExamineAuditData.leaderName,
                "leaderNo": hazardExamineAuditData.leaderNo,
                "questionDescription": hazardExamineAuditData.questionDescription,
                "rectificationDeadline": hazardExamineAuditData.rectificationDeadline,
                "rectificationRequire": hazardExamineAuditData.rectificationRequire,
                "feedbackContent": hazardExamineAuditData.feedbackContent,
                "feedbackDate": hazardExamineAuditData.feedbackDate,
                "auditUserId": hazardExamineAuditData.auditUserId,
                "auditRealName": hazardExamineAuditData.auditRealName,
                "auditRemark": hazardExamineAuditData.auditRemark,
                "auditDate": hazardExamineAuditData.auditDate,
                "status": hazardExamineAuditData.status,
                "recordUserId": hazardExamineAuditData.recordUserId,
                "recordRealName": hazardExamineAuditData.recordRealName,
                "recordDate": hazardExamineAuditData.recordDate,
                "rectifyReturnRecordList": hazardExamineAuditData.rectifyReturnRecordList,
            });
            form.render('checkbox');
        }
    }


    form.on('submit(hazardExamineAudit-edit-form-submit)', function (data) {
        data.field.auditResult = '1';
        //通过，检查记录status——修改为审核通过
        data.field.status = '6';
        //安全检查发布hazard_examine-status——已结束,改为定时任务，根据检查截至时间修改状态为已结束
        // data.field.examineStatus='4';
        addOrUpdate(data.field);
        return false;
    });

    form.on('submit(hazardExamineAudit-edit-form-return)', function (data) {
        //退回
        if(commonJS.isEmpty(data.field.auditRemark)){
            febs.alert.warn('退回时审核备注不能为空！');
            return false;
        }
        data.field.auditResult = '2';
        // 检查记录status——修改为待整改
        data.field.status = '2';
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        data.tenantId = tenantId;
        data.examineId = examineId;
        data.examineRecordId = examineRecordId;

        if (data.examineRecordId && data.examineRecordId.length > 0) {
            febs.modal.confirm('提交审核信息', '确定提交审核信息？', function (index) {
                febs.postArray(ctx + 'hazardExamineAudit/audit', data, function () {
                    layer.closeAll();
                    febs.alert.success('提交成功');
                    $('#febs-hazardExamineAudit').find('#query').trigger('click');
                });
            });

        }

    }

});